﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using System.IO;
using System.Text;

namespace YiDaShuiHang
{
    /// <summary>
    /// 接受/发送消息帮助类
    /// </summary>
    public class messageHelp
    {
        //返回消息
        public string ReturnMessage(string postStr)
        {
            string responseContent = "";
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(postStr);//读取xml字符串
            XmlElement root = doc.DocumentElement;
            XmlNode MsgType = root.SelectSingleNode("MsgType");//获取收到的消息类型。文本(text)，图片(image)，语音等。          
            if (MsgType != null)
            {
                switch (MsgType.InnerText)
                {
                    case "event":
                        responseContent = EventHandle(doc);//事件处理
                        break;
                    case "text":
                        responseContent = TextHandle(doc);//接受文本消息处理
                        break;
                    default:
                        break;
                }
            }
            return responseContent;
        }


 
        //事件
        public string EventHandle(XmlDocument xmldoc)
        {
          
                string responseContent = "";
                XmlNode Event = xmldoc.SelectSingleNode("/xml/Event");
                XmlNode EventKey = xmldoc.SelectSingleNode("/xml/EventKey");
                XmlNode ToUserName = xmldoc.SelectSingleNode("/xml/ToUserName");
                XmlNode FromUserName = xmldoc.SelectSingleNode("/xml/FromUserName");
             
                if (Event != null && Event.InnerText.Equals("CLICK"))
                {                   
                    try
                    {
                        switch (EventKey.InnerText)
                        {
                            case "A001":
                                #region
                                /*
                                     string OrderUrl = "";
                                      string ResultUrl = "";
                                      string ErrorMessage = "";
                                      string FlowGroupCode = "JMGS";
                                     GetUrl.OrderCom con = new GetUrl.OrderCom();
                                     if (con.GetOrderUrl(FlowGroupCode, FromUserName.InnerText, ref  OrderUrl, ref  ResultUrl, ref  ErrorMessage))
                                     {
                                         responseContent = responseContent = string.Format(ReplyType.Message_News_Main,
                                         FromUserName.InnerText,
                                         ToUserName.InnerText,
                                         DateTime.Now.Ticks,
                                        "3",
                                     string.Format(ReplyType.Message_News_Item, "预约选项有效时间为30分钟，逾期操作无效！请单击选择以下功能：",
                                     "",
                                     "",
                                     "")+string.Format(ReplyType.Message_News_Item, "1.窗口预约：",
                                     "",
                                        "http://120.237.31.12/GSJ_WX/images/icon2.gif",
                                     OrderUrl)+ string.Format(ReplyType.Message_News_Item, "2.查看或取消预约：",
                                     "",
                                        "http://120.237.31.12/GSJ_WX/images/icon1.png",
                                     ResultUrl)                                     
                                     );
                                     }
                                     else {
                                         responseContent = string.Format(ReplyType.Message_Text,
                             FromUserName.InnerText,
                             ToUserName.InnerText,
                             DateTime.Now.Ticks,
                            "此服务暂停。");

                                     }
                           
                          
                               
                                 * */
                                #endregion
                                break;
                            case "A002":                         
                              #region
                                  string url1 = "http://gsxt.gdgs.gov.cn/aiccips/";
                                  string url2 = "http://sspt.jiangmen.gov.cn:8085/commercialSubject/toIndex";
                                  string url3 = "http://sspt.jiangmen.gov.cn:8085/jmxwqy/xwqy/toIndex";
                                  string url4 = "http://120.237.31.12/GSJ_WX/list.aspx?type=show";
                                  string url5 = "http://120.237.31.12/GSJ_WX/list.aspx?type=warn";
                                    responseContent = responseContent = string.Format(ReplyType.Message_News_Main,
                                    FromUserName.InnerText,
                                    ToUserName.InnerText,
                                    DateTime.Now.Ticks,
                                   "3",
                                    string.Format(ReplyType.Message_News_Item, "请点击您需要查看的内容：",
                                     "",
                                     "",
                                     "") +  string.Format(ReplyType.Message_News_Item, "1.行政处罚信息公示：",
                                "",
                                   "http://120.237.31.12/GSJ_WX/images/4.jpg",
                                url4) + string.Format(ReplyType.Message_News_Item, "2.案例警示：",
                                "",
                                   "http://120.237.31.12/GSJ_WX/images/5.jpg",
                                url5)
                            );
          
                          
                               #endregion

                                // FromUserName.InnerText,
                                // ToUserName.InnerText,
                                // DateTime.Now.Ticks,
                                // "2",
                                //  string.Format(ReplyType.Message_News_Item, "我要寄件", "",
                                //  "http://www.soso.com/orderPlace.jpg",
                                //  "http://www.soso.com/") +
                                //  string.Format(ReplyType.Message_News_Item, "订单管理", "",
                                //  "http://www.soso.com/orderManage.jpg",
                                //  "http://www.soso.com/"));
                              
                                break;
                            case "A003":
                                #region
                                //responseContent = string.Format(ReplyType.Message_News_Main,
                                //    FromUserName.InnerText,
                                //    ToUserName.InnerText,
                                //    DateTime.Now.Ticks,
                                //    "1",
                                //     string.Format(ReplyType.Message_News_Item, "标题", "摘要",
                                //     "http://www.soso.com/jieshao.jpg",
                                //     "http://www.soso.com/"));
                                responseContent = string.Format(ReplyType.Message_Text,
                                 FromUserName.InnerText,
                                 ToUserName.InnerText,
                                 DateTime.Now.Ticks,
                                 "功能正在开发中。。。");
                                #endregion
                                break;
                            case "B001":
                                #region
                                responseContent = string.Format(ReplyType.Message_Text,
                                 FromUserName.InnerText,
                                 ToUserName.InnerText,
                                 DateTime.Now.Ticks,
                                 "功能正在开发中。。。");
                                #endregion
                                break;
                            case "B002":
                                #region
                                responseContent = string.Format(ReplyType.Message_Text,
                                 FromUserName.InnerText,
                                 ToUserName.InnerText,
                                 DateTime.Now.Ticks,
                                 "功能正在开发中。。。");
                                #endregion
                                break;
                            case "e1":
                                #region
                                responseContent = string.Format(ReplyType.Message_Text,
                                 FromUserName.InnerText,
                                 ToUserName.InnerText,
                                 DateTime.Now.Ticks,
                                 "1．问：什么是商事登记？");
                                #endregion
                                break;
                            case "B004":
                                #region
                                responseContent = string.Format(ReplyType.Message_Text,
                                 FromUserName.InnerText,
                                 ToUserName.InnerText,
                                 DateTime.Now.Ticks,
                                 "功能正在开发中。。。");
                                #endregion
                                break;
                            case "C001":
                                #region
                                responseContent = string.Format(ReplyType.Message_Text,
                                 FromUserName.InnerText,
                                 ToUserName.InnerText,
                                 DateTime.Now.Ticks,
                                 "功能正在开发中。。。");
                                #endregion
                                break;
                            case "C002":
                                #region
                                responseContent = string.Format(ReplyType.Message_Text,
                                 FromUserName.InnerText,
                                 ToUserName.InnerText,
                                 DateTime.Now.Ticks,
                                 "功能正在开发中。。。");
                                #endregion
                                break;
                            case "C003":
                                #region
                                responseContent = string.Format(ReplyType.Message_Text,
                                 FromUserName.InnerText,
                                 ToUserName.InnerText,
                                 DateTime.Now.Ticks,
                                 "功能正在开发中。。。");
                                #endregion
                                break;
                            case "C004":
                                #region
                                responseContent = string.Format(ReplyType.Message_Text,
                                 FromUserName.InnerText,
                                 ToUserName.InnerText,
                                 DateTime.Now.Ticks,
                                 "功能正在开发中。。。");
                                #endregion
                                break;
                            case "C005":
                                #region
                                responseContent = string.Format(ReplyType.Message_Text,
                                 FromUserName.InnerText,
                                 ToUserName.InnerText,
                                 DateTime.Now.Ticks,
                                 "功能正在开发中。。。");
                                #endregion
                                break;
                            case "D001":
                                #region
                                string u1 = "http://gsxt.gdgs.gov.cn/aiccips/";
                                string u2 = "http://sspt.jiangmen.gov.cn:8085/commercialSubject/toIndex";
                                string u3 = "http://sspt.jiangmen.gov.cn:8085/jmxwqy/xwqy/toIndex";
           
                                responseContent = responseContent = string.Format(ReplyType.Message_News_Main,
                                FromUserName.InnerText,
                                ToUserName.InnerText,
                                DateTime.Now.Ticks,
                               "4",
                                string.Format(ReplyType.Message_News_Item, "请点击您需要查看的内容：",
                                 "",
                                 "",
                                 "") + string.Format(ReplyType.Message_News_Item, "1.全国企业信用信息公示系统：",
                            "",
                               "http://120.237.31.12/GSJ_WX/images/1.jpg",
                            u1) + string.Format(ReplyType.Message_News_Item, "2.江门市商事主体信息公示平台：",
                            "",
                               "http://120.237.31.12/GSJ_WX/images/2.jpg",
                            u2) + string.Format(ReplyType.Message_News_Item, "3.小微企业名录库：",
                            "",
                               "http://120.237.31.12/GSJ_WX/images/3.jpg",
                            u3)
                        );
                                #endregion
                                break;

                        }
                    }catch(Exception ex){
                        WriteLog(ex.Message);
                    }
                }
                return responseContent;
               
    
            }
        //接受文本消息
        public string TextHandle(XmlDocument xmldoc)
        {
            string responseContent = "";
            XmlNode ToUserName = xmldoc.SelectSingleNode("/xml/ToUserName");
            XmlNode FromUserName = xmldoc.SelectSingleNode("/xml/FromUserName");
            XmlNode Content = xmldoc.SelectSingleNode("/xml/Content");
            if (Content != null)
            {
                responseContent = string.Format(ReplyType.Message_Text,
                    FromUserName.InnerText,
                    ToUserName.InnerText,
                    DateTime.Now.Ticks,
                   "温馨提示\n亲们，首先非常感谢您对我们“江门工商”微信公众号的关注与支持！因腾讯系统设定的原因，信息只能保存48小时。因此，为保证您发来的咨询信息能及时获得解答，请您尽量在周一至周五上午期间发来，我们将有专人跟进解答。如您需要进行消费申诉维权、投诉举报不法经营行为等，请致电12315或12345，我们将及时跟进处理。");
            }
            return responseContent;
        }

        //写入日志
        public void WriteLog(string text)
        {
            StreamWriter sw = new StreamWriter(HttpContext.Current.Server.MapPath(".") + "\\log.txt", true);
            sw.WriteLine(text);
            sw.Close();//写入
        }
    }

    //回复类型
    public class ReplyType
    {
        /// <summary>
        /// 普通文本消息
        /// </summary>
        public static string Message_Text
        {
            get
            {
                return @"<xml>
                            <ToUserName><![CDATA[{0}]]></ToUserName>
                            <FromUserName><![CDATA[{1}]]></FromUserName>
                            <CreateTime>{2}</CreateTime>
                            <MsgType><![CDATA[text]]></MsgType>
                            <Content><![CDATA[{3}]]></Content>
                            </xml>";
            }
        }
        /// <summary> 
        /// 图文消息主体
        /// </summary>
        public static string Message_News_Main
        {
            get
            {
                return @"<xml>
                            <ToUserName><![CDATA[{0}]]></ToUserName>
                            <FromUserName><![CDATA[{1}]]></FromUserName>
                            <CreateTime>{2}</CreateTime>
                            <MsgType><![CDATA[news]]></MsgType>
                            <ArticleCount>{3}</ArticleCount>
                            <Articles>
                            {4}
                            </Articles>
                            </xml> ";
            }
        }
        /// <summary>
        /// 图文消息项
        /// </summary>
        public static string Message_News_Item
        {
            get
            {
                return @"<item>
                            <Title><![CDATA[{0}]]></Title> 
                            <Description><![CDATA[{1}]]></Description>
                            <PicUrl><![CDATA[{2}]]></PicUrl>
                            <Url><![CDATA[{3}]]></Url>
                            </item>";
            }
        }
    }
}